import { proxy, reqHeader, isAuthorization, followAction} from '../../utils/util.js';
import LoginStore from '../../pages/login/store.js';
const getDefaultData = ()=>{
  return {
    loginUserId: '',
    listData: [],
    page: 1,
    hasNext: true
  }
}
Component({
  properties: {
    popParams: {
      type: Object,
      value: '',
      observer: function (newVal, oldVal, changedPath) {
        this._loadData(newVal);
        this.setData({
          titleNum: newVal.count
        })
      }
    }
  },
  data: getDefaultData(),

  /**
   * 组件的方法列表
   */
  methods: {
    scrollList:function(){
      if (this.data.hasNext){
        this._loadData(this.properties.popParams);
      }
      
    },
    followAction: function (e) {
      let me = this;
      let listData = me.data.listData;
      let index = e.currentTarget.dataset.key;
      let userInfo = listData[index];
      let callback = () => {
        me.setData(getDefaultData(), () => {
          me._loadData(me.properties.popParams);
        })
      }
      let resback = () => {
        let obj = {
          listData: listData
        }
        if (this.properties.popParams.popKey == 'follow'){
          let num = this.data.titleNum;
          let curUserId = this.properties.popParams.curUserId;
          let loginUserId = LoginStore.userInfo.userId;
          if (curUserId == loginUserId) {
            if (userInfo.userFolloweeState) {
              num++
            } else {
              num--;
            }
            obj.titleNum = num;
          }
        }
        
        me.setData(obj, function () {
          wx.hideLoading();
        });
      }
      followAction(e, me, userInfo, "socialList", callback, resback);

    },
    closePop: function () {
      this.triggerEvent('closepopevent', {}, { bubbles: true, composed: true })
    },
    _loadData:function(popParams){
      let me = this;
      let userAccountInfo = LoginStore.userInfo;
      let urlStr = `${proxy}/community/res/relation/${popParams.curUserId}/${popParams.popKey}` ;
      
      wx.request({
        url: urlStr,
        data: {
          // startTime:'',
          page: this.data.page,
          size: 20,
          type: 'json',
          access_token: userAccountInfo.access_token
        },
        header: reqHeader,
        success: function (res) {
          if (res.statusCode == 200){
            let hasNext = true;
            let list = res.data;
            if (popParams.popKey === "liked"){
              list = res.data.pictureLikeeds;
            } 
            if (!list.length){
              hasNext = false;
            }
            let listData = me.data.listData.concat(list);
            me.setData({
              loginUserId:userAccountInfo.userId || '',
              listData: listData,
              page:++me.data.page,
              hasNext: hasNext
            })
          }else{
            console.log(res.errMsg);
          }
        }
      });
    }
  }
})
